package com.vik.webgallery.web;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Properties;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vik.webgallery.beans.Gallery;
import com.vik.webgallery.beans.Photo;
import com.vik.webgallery.beans.User;
import com.vik.webgallery.persistence.CommentMapper;
import com.vik.webgallery.persistence.GalleryMapper;
import com.vik.webgallery.persistence.PhotoMapper;
import com.vik.webgallery.persistence.UserMapper;

@Controller
public class UserController {
	
	private static final Logger logger = LoggerFactory.getLogger(GalleryController.class);
	
	@Resource(name = "appConfig")
	private Properties appProperties;

	@Autowired
	public GalleryMapper galleryMapper;
	
	@Autowired
	public UserMapper userMapper;
	
	@Autowired
	public PhotoMapper photoMapper;
	
	@Autowired
	public CommentMapper commentMapper;
	
	
	@RequestMapping(value="user/deletegallery/{userName}/{galleryId}", method=RequestMethod.GET)
	public String deleteGallery(@PathVariable String userName, @PathVariable long galleryId, Model model, Principal principal){
		
		// Kontrola opravneni
		if (!userName.equals(principal.getName())){
			// TODO chybova zprava
			return "/user/showUserProfile";
		}
		
		// Nacteni galerie k odstraneni
		Gallery gallery = galleryMapper.getGallery(galleryId);
		
		if (gallery == null){
			// TODO zobrazit chybovou zpravu - galerie neexistuje
			return "/user/showAdminProfile";
		}
		
		gallery.setOwner(userMapper.getUserById(gallery.getOwnerId()));
		ArrayList<Photo> photos = photoMapper.getPhotosByGalleryId(gallery.getId());
		
		// Odstraneni komentaru a fotografii z db, smazani souboru
		for (Photo p: photos){
			p.setGallery(gallery);
			commentMapper.deleteCommentsByPhotoId(p.getId());
			photoMapper.delete(p);
			p.deleteFiles((String)appProperties.get("UPLOAD_DIR"));
		}
		
		// Odstraneni galerie
		galleryMapper.deleteGallery(gallery.getId());
		gallery.deleteFolders((String)appProperties.get("UPLOAD_DIR"));
		
		logger.info("INFO: deleted");
		logger.error("ERROR: deleted");
		
		return "redirect:/user/show/" + userName;
	}
	
	@RequestMapping(value="user/show/{userName}", method=RequestMethod.GET)
	public String showUserProfile(@PathVariable String userName, Model model, Principal principal){

		String loggedUserName = "";
		
		if (principal != null) loggedUserName = principal.getName();
		
		logger.info("Showing user profile");
		
		// Load user details
		User user = userMapper.getUser(userName);
		model.addAttribute("user", user);
					
		// Load galleries
		ArrayList<Gallery> galleries = galleryMapper.getUserGalleries(userMapper.getUser(userName).getId());
		for (Gallery g: galleries){
			g.setOwner(userMapper.getUserById(g.getOwnerId()));
		}
		model.addAttribute("galleries", galleries);
		
		// TODO load owners
		
		if (userName.equals(loggedUserName)){
			// Show admin profile
			return "/user/showAdminProfile";
		}else{
			// Show guest profile
			return "/user/showGuestProfile";
		}
	}
	
	
	
}

